package com.lovo.roomProject.system.dao.impl;

import com.lovo.roomProject.dao.BaseDAO;
import com.lovo.roomProject.pojo.SystemDO;
import com.lovo.roomProject.system.dao.SystemDAO;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SystemDAOImpl extends BaseDAO implements SystemDAO {
    @Override
    public List listSystem() {
        this.getConnection();
        String sql = "SELECT * FROM t_user";
        List list = new ArrayList();
        try {
            statement = con.prepareStatement(sql);
            rs = statement.executeQuery();
            while (rs.next()) {
                SystemDO systemDO = new SystemDO();
                systemDO.setId(rs.getInt("u_id"));
                systemDO.setAccount(rs.getString("u_account"));
                systemDO.setPassword(rs.getString("u_password"));
                systemDO.setName(rs.getString("u_name"));
                systemDO.setLevel(rs.getString("u_level"));
                list.add(systemDO);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement);
        }
        return list;
    }

    @Override
    public void insertSystem(SystemDO systemDO) {
        this.getConnection();
        String sql = "INSERT INTO t_user(u_id,u_account,u_password,u_name,u_level) values (?,?,?,?,?)";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, systemDO.getId());
            statement.setObject(2, systemDO.getAccount());
            statement.setObject(3, systemDO.getAccount() + "123");
            statement.setObject(4, systemDO.getName());
            statement.setObject(5, systemDO.getLevel());
            statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement);
        }
    }

    /**
     * 修改等级或账号
     *
     * @param systemDO
     */
    @Override
    public void updateSystem(SystemDO systemDO) {
        this.getConnection();
        String sql = "UPDATE t_user SET u_account = ?,u_password = ?,u_level = ? WHERE u_id = ?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, systemDO.getAccount());
            statement.setObject(2, systemDO.getPassword());
            statement.setObject(3, systemDO.getLevel());
            statement.setObject(4, systemDO.getId());
            statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement);
        }
    }

    @Override
    public SystemDO getById(int systemId) {
        this.getConnection();
        String sql = "SELECT * FROM  t_user WHERE u_id = ?";
        SystemDO systemDO = new SystemDO();
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, systemId);
            rs = statement.executeQuery();
            if (rs.next()) {
                systemDO.setId(rs.getInt("u_id"));
                systemDO.setAccount(rs.getString("u_account"));
                systemDO.setPassword(rs.getString("u_password"));
                systemDO.setName(rs.getString("u_name"));
                systemDO.setLevel(rs.getString("u_level"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
//            close(con, statement, rs);
        }
        return systemDO;
    }

    @Override
    public void deleteSystem(int systemId, SystemDO loginUser) throws Exception {
        this.getConnection();
        SystemDO systemDO = getById(systemId);
        if (systemDO.getAccount().equals(loginUser.getAccount())) {
            throw new Exception("不能删除自己");
        }
        String sql = "DELETE FROM t_user WHERE u_id = ?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, systemId);
            statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement);
        }
    }

}
